﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using NTTDATA.BEENChina.ResultCertification.Split;
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0215", Name = "期间项目_间隔检证", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0215 : VerfityOneResult
    {
        private int _monthSpan = 6;
        /// <summary>
        ///         
        /// </summary>
        [Category("期间间隔,单位:月"), Description("输入间隔值")]
        public int MonthSpan
        {
            get { return _monthSpan; }
            set { _monthSpan = value; }
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false; 

            foreach (List<string> row in result)
            {
                if (row == null || row.Count() < 2)
                {
                    continue;
                }
                List<string> changeResult = new DateHelper(ChildImageObject.CategorySetting).ChangeDateStr(row);

                //如果结果有空白 忽略
                if (EmptyColumnCount(changeResult)>0)
                {
                    continue;
                }

                string firstDateStr = changeResult[0];
                string secondDateStr = changeResult[1];
               // complementEachOther(ref firstDateStr, ref secondDateStr);

                var completeArray = base.CompleteDate(new List<string>{firstDateStr, secondDateStr});

                firstDateStr = completeArray[0];
                secondDateStr = completeArray[1]; 

                DateTime? fistDate = GetEntryDate(firstDateStr);
                DateTime? secondDate = GetEntryDate(secondDateStr);

                if (fistDate == null || secondDate == null)
                {
                    continue;
                }

                if (monthSpan(fistDate.Value, secondDate.Value) > _monthSpan)
                {
                    bresult = true;
                    break;
                }                
            }
            if (bresult)
            {
                System.Console.WriteLine("VerfityRQ0215");
            }

            return bresult;
        }

       /* private void complementEachOther( ref string date1, ref string date2)
        {
            string[] date1Array = date1 == null ? new string[0]:date1.Split(new char[] { '/' });
            string[] date2Array = date2 == null ? new string[0] : date2.Split(new char[] { '/' });
            
            int maxlen = date1Array.Length  >date2Array.Length  ? date1Array.Length : date2Array.Length;
            string[] date1Result = new string[maxlen];
            string[] date2Result = new string[maxlen];

            for (int i = 0; i < maxlen; i++)
            {
                date1Result[i] = i >= date1Array.Length || string.IsNullOrEmpty(date1Array[i])  ? 
                                    (i < date2Array.Length ? date2Array[i] :"" ) : date1Array[i];
                date2Result[i] = i >= date2Array.Length || string.IsNullOrEmpty(date2Array[i] )?
                                    (i < date1Array.Length ? date1Array[i] : "") : date2Array[i];
            }

            date1Result = date1Result.Select(t => string.IsNullOrEmpty(t) ? "01" : t).ToArray();
            date2Result = date2Result.Select(t => string.IsNullOrEmpty(t) ? "01" : t).ToArray();

            date1 = string.Join("/", date1Result);
            date2 = string.Join("/", date2Result);
        }*/

        private DateTime? GetEntryDate(string entryResult)
        {
            if (string.IsNullOrEmpty(entryResult))
            {
                return null;
            }

            entryResult = CompleteDate( entryResult );

            var splitEntryResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat,entryResult);
            DateTime? entryDate = splitEntryResult.GetDateTime();

            return entryDate;
        }

        private int monthSpan(DateTime fistDate, DateTime secondDate)
        {
            return (secondDate.Year - fistDate.Year) * 12 + (secondDate.Month - fistDate.Month);
        }

    }
}
